//
//  KLSetPayPasswordViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/7/23.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLSetPayPasswordViewController.h"
#import "KLHelp.h"
#import "KLHttpAPI.h"
#import "Masonry.h"
#import "TTPasswordView.h"
#import "TTConst.h"
#import "JRToast.h"

@interface KLSetPayPasswordViewController ()
/**
 *  请您设置仓库提示的label
 */
@property(nonatomic,retain)UILabel *label;
/**
 *  请再次输入密码的提示
 */
@property(nonatomic,retain)UILabel *secondtip;
/**
 *   密码框
 */
@property(nonatomic,retain)TTPasswordView *password;
/**
 *  第一次输入的密码
 */
@property(nonatomic,copy)NSString *firstCode;


/**
 *  判断入口类型:备用
 */
@property(nonatomic,copy)NSString *enterType;
@end

@implementation KLSetPayPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化界面
    [self uiSet];
    //初始化导航栏
    [self navSet];
}

#pragma mark - 界面设置
-(void)uiSet{
    self.view.backgroundColor = RGB_UI_COLOR(238, 238, 238);
    
    self.label=[[UILabel alloc]init];
    self.label.text=@"请设置支付密码，以保障您的资金安全";
    self.label.textColor=UIColorFromRGB(0x3e3e3e);
    self.label.font=[UIFont systemFontOfSize:13];
    self.label.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:self.label];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(80);
        make.centerX.equalTo(self.view).with.offset(0);
    }];
    
    self.password = [[TTPasswordView alloc]init];
    self.password.elementCount = 6;
    self.password.elementColor=UIColorFromRGB(0xd5d5d5);
    [self.view addSubview:self.password];
    __block KLSetPayPasswordViewController *weakself=self;
    self.password.passwordBlock = ^(NSString *password) {
        if (password.length==6) {
            [weakself enterCode:password];
        }
    };
    [self.password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.label).with.offset(60);
        make.centerX.equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(300);
        make.height.mas_equalTo(50);
    }];
    
    self.secondtip=[[UILabel alloc]init];
    self.secondtip.text=@"请再次输入您的安全码";
    self.secondtip.textColor=UIColorFromRGB(0xff3674);
    self.secondtip.font=[UIFont systemFontOfSize:13];
    self.secondtip.textAlignment=NSTextAlignmentCenter;
    self.secondtip.hidden=YES;
    [self.view addSubview:self.secondtip];
    [self.secondtip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.password).with.offset(60);
        make.centerX.equalTo(self.view).with.offset(0);
    }];
}

-(void)enterCode:(NSString *)code{
    if (!self.firstCode) {
        self.firstCode=code;
        self.secondtip.text=@"请再次输入您的支付密码";
        self.secondtip.textColor=UIColorFromRGB(0x3e3e3e);;
        self.secondtip.hidden=NO;
        [self.password clearText];
        [self.password.textField becomeFirstResponder];
    }else if (self.firstCode&&[self.firstCode isEqualToString:code]){
        [KLHttpAPI setPayPasswordWithPassword:self.firstCode completionHandler:^(MKNetworkOperation *operation) {
            NSLog(@"保存支付密码返回信息:%@",operation.responseString);
            NSDictionary *dic = operation.responseJSON;
            NSString *isSuccess = [dic objectForKey:@"success"];
            if(isSuccess.integerValue == 1){
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isPaypwdExit"];
                [[NSUserDefaults standardUserDefaults] setObject:code forKey:@"paypassword"];
                [JRToast showWithText:@"设置成功"];
            }else{
                [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"isPaypwdExit"];
                [JRToast showWithText:[dic objectForKey:@"msg"]];
            }
        } errorHandler:^(NSString *error) {
        }];
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        self.secondtip.textColor=UIColorFromRGB(0xff3674);
        self.secondtip.text=@"您两次输入的密码不一致，请重新设置";
        [self.password clearText];
        [self.password.textField becomeFirstResponder];
        self.firstCode=nil;
    }
}

#pragma mark -设置导航栏和标头视图
-(void)navSet{
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_View_Width, 64)];
    navView.backgroundColor = [UIColor colorWithRed:(float)8/255 green:(float)128/255 blue:(float)181/255 alpha:1];
    [self.view addSubview:navView];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"设置支付密码";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    [navView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(navView).with.offset(0);
        make.bottom.mas_equalTo(-12);
    }];
    
    UIImageView *backbutton = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"返回"]];
    backbutton.contentMode = UIViewContentModeScaleAspectFit;
    backbutton.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doBack:)];
    [backbutton addGestureRecognizer:tap];
    [navView addSubview:backbutton];
    [backbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-12);
        make.left.mas_equalTo(9);
        make.width.mas_equalTo(25);
        make.height.mas_equalTo(20);
    }];
}-(void)doBack:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
